package net.maku.project.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.project.convert.ProcessStatesConvert;
import net.maku.project.entity.ProcessStatesEntity;
import net.maku.project.query.ProcessStatesQuery;
import net.maku.project.service.ProcessStatesService;
import net.maku.project.vo.*;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程状态表
 *
 * @author 艾力 1106997025@qq.com
 * @since 1.0.0 2024-07-09
 */
@RestController
@RequestMapping("project/process-states")
@Tag(name = "流程状态表")
@AllArgsConstructor
public class ProcessStatesController {
    private final ProcessStatesService processStatesService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('project:process-states:page')")
    public Result<PageResult<ProcessStatesVO>> page(@ParameterObject @Valid ProcessStatesQuery query) {
        PageResult<ProcessStatesVO> page = processStatesService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('project:process-states:info')")
    public Result<ProcessStatesVO> get(@PathVariable("id") Long id) {
        ProcessStatesEntity entity = processStatesService.getById(id);

        return Result.ok(ProcessStatesConvert.INSTANCE.convert(entity));
    }

    @GetMapping("{projectId}/{stageId}")
    @Operation(summary = "查询当前项目和阶段的流程环节")
    @PreAuthorize("hasAuthority('project:process-states:list')")
    public Result<List<ProcessStatesVO>> get(@PathVariable("projectId") Long projectId, @PathVariable("stageId") Long stageId) {

        List<ProcessStatesVO> processStagesStatesVOS = processStatesService.getProcessStageStates(projectId, stageId);
        return Result.ok(processStagesStatesVOS);
    }

//    @PostMapping
//    @Operation(summary = "保存")
//    @PreAuthorize("hasAuthority('project:process-states:save')")
//    public Result<String> save(@RequestBody ProcessStatesVO vo){
//        processStatesService.save(vo);
//
//        return Result.ok();
//    }

    //    @PutMapping
//    @Operation(summary = "修改")
//    @PreAuthorize("hasAuthority('project:process-states:update')")
//    public Result<String> update(@RequestBody @Valid ProcessStatesVO vo){
//        processStatesService.update(vo);
//
//        return Result.ok();
//    }
    @PutMapping
    @Operation(summary = "修改状态")
    @PreAuthorize("hasAuthority('project:process-states:update')")
    public Result<String> changeStates(@RequestBody @Valid ProcessStatesVO vo) {
        processStatesService.changeStates(vo);

        return Result.ok();
    }
    @PutMapping("reset")
    @Operation(summary = "重置环节信息")
    @PreAuthorize("hasAuthority('project:process-states:reset')")
    public Result<String> reset(@RequestBody @Valid ProcessStatesVO vo) {

        processStatesService.update(vo);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('project:process-states:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        processStatesService.delete(idList);
        return Result.ok();
    }

    @GetMapping("project")
    @Operation(summary = "查询流程状态")
    // @PreAuthorize("hasAuthority('project:process-states:list')")
    public Result<List<ProcessStatesVO>> getProcessStates(@RequestParam("projectId") Long projectId) {
        List<ProcessStatesVO> processStatesVOS = processStatesService.getProcessStates(projectId);
        return Result.ok(processStatesVOS);
    }
    @GetMapping("statusStatistics")
    @Operation(summary = "查询流程状态统计")
    // @PreAuthorize("hasAuthority('project:process-states:list')")
    public Result<List<StatesCountVO>> getStatesCount() {
        List<StatesCountVO> countVOS = processStatesService.getStatesCount();
        return Result.ok(countVOS);
    }

    @GetMapping("/status")
    public Result<List<StateSelectVO>> getProcessStatesByProjectId(@RequestParam("projectId") Long projectId) {
        List<StateSelectVO> selectVOS = processStatesService.getProcessStatesByProjectId(projectId);
        return Result.ok(selectVOS);
    }

    @PostMapping("/status/update")
    public Result<String> updateProcessStates(@RequestBody @Valid UpdateStateVO updateStateVO) {
        processStatesService.updateProcessStates(updateStateVO);
        return Result.ok();
    }




}